﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Program11
{
    class Program
    {
        static void Main(string[] args)
        {
            SClass(GetPersons());
            Console.WriteLine("已存入本地");

            #region 老的版本
            //while (true)
            //{
            //    if (num > 1) { break; }
            //    Console.WriteLine("请输入工号：");
            //    no = Console.ReadLine();
            //    Console.WriteLine("请输入姓名：");
            //    name = Console.ReadLine();
            //    //Console.WriteLine("请输入性别：");
            //    //sex = Convert.ToBoolean(Console.ReadLine());
            //    Console.WriteLine("请输入生日：");
            //    birthday = Convert.ToDateTime(Console.ReadLine());
            //    people.Add(People(no, name, true, birthday));
            //    num++;
            //}
            //for (int i = 0; i < num; i++)
            //{
            //    SClass(people[i]);
            //}
            #endregion

            Console.WriteLine("开始读取本地数据.....\r\n");

            for (int i = 0; i < GetPersons().Count+1; i++)
            {
                Console.Write("工号：" + DClass()[i].EmployeeNo + "  姓名：" + DClass()[i].Name + "  性别：" + DClass()[i].Sex + "  生日：" + DClass()[i].Birthday);
                Console.WriteLine();
            }

            Console.ReadLine();
        }

        /// <summary>
        /// 将对象序列化
        /// </summary>
        /// <param name="p"></param>
        public static void SClass(List<Person> people)
        {
            using (FileStream fsWrite = new FileStream("student.db", FileMode.OpenOrCreate, FileAccess.Write))
            {
                //把对象内容转换成二进制，序列化
                var buffer = new BinaryFormatter();
                buffer.Serialize(fsWrite, people);
            }
        }

        /// <summary>
        /// 将对象反序列化
        /// </summary>
        public static List<Person> DClass()
        {
            List<Person> people = new List<Person>();
            using (FileStream fsRead = new FileStream("student.db", FileMode.OpenOrCreate, FileAccess.Read))
            {
                var buffer = new BinaryFormatter();
                people = (List<Person>)buffer.Deserialize(fsRead);
            }
            return people;
        }

        /// <summary>
        /// 随机初始化不少于100个员工
        /// </summary>
        /// <returns></returns>
        public static List<Person> GetPersons()
        {
            string fullName="";
            Random rd = new Random();
            List<Person> people = new List<Person>();

            for (int i = 0; i < rd.Next(100,200); i++)
            {
                Person p = new Person();
                p.EmployeeNo = i.ToString();
                GetRandomChineseName(ref fullName);
                p.Name = fullName;
                p.Sex = Convert.ToBoolean(rd.Next(0, 2));
                p.Birthday = DateTime.Today;
                people.Add(p);
            }
            return people;
        }
        /// <summary>
        /// 随机生成一些中文姓名
        /// </summary>
        /// <param name="fullName"></param>
        public static void GetRandomChineseName(ref string fullName)
        {
            string[] _fNames = new string[]
            {
                "白","毕","卞","蔡","曹","岑","常","车","陈","成" ,"程","池","邓","丁","范","方","樊","费","冯","符"
                ,"傅","甘","高","葛","龚","古","关","郭","韩","何" ,"贺","洪","侯","胡","华","黄","霍","姬","简","江"
                ,"姜","蒋","金","康","柯","孔","赖","郎","乐","雷" ,"黎","李","连","廉","梁","廖","林","凌","刘","柳"
                ,"龙","卢","鲁","陆","路","吕","罗","骆","马","梅" ,"孟","莫","母","穆","倪","宁","欧","区","潘","彭"
                ,"蒲","皮","齐","戚","钱","强","秦","丘","邱","饶" ,"任","沈","盛","施","石","时","史","司徒","苏","孙"
                ,"谭","汤","唐","陶","田","童","涂","王","危","韦" ,"卫","魏","温","文","翁","巫","邬","吴","伍","武"
                ,"席","夏","萧","谢","辛","邢","徐","许","薛","严" ,"颜","杨","叶","易","殷","尤","于","余","俞","虞"
                ,"元","袁","岳","云","曾","詹","张","章","赵","郑" ,"钟","周","邹","朱","褚","庄","卓"

            };
            string _lNames = "病毒没有国界疫情不分种族唯有团结协作携手应对国际社会才能战胜疫情维护人类共同家园疫情发生以来中方始终秉持人类命运共同体理念本着公开透明负责任态度及时发布疫情信息毫无保留同世界卫生组织和国际社会分享防控治疗经验加强科研攻关合作";

            //通过系统时间定义随机种子
            var rd = new Random(DateTime.Now.Millisecond);
            //在姓氏中随机获取一个字
            var fName = _fNames[rd.Next(_fNames.Length - 1)];
            //在名中随机获取两个字
            var lName = _lNames.Substring(rd.Next(0, _lNames.Length - 1), 1) + _lNames.Substring(rd.Next(0, _lNames.Length - 1), 1);
            fullName = fName + lName;
            Thread.Sleep(1);
        }
    }
}
